package com.facebookproject.graph.getlist;

import static com.facebookproject.Const.*;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.view.View;
import android.widget.Toast;

import com.facebookproject.R;
import com.facebookproject.graph.Holder;
import com.facebookproject.graph.element.ElementAdapterListView;
import com.facebookproject.graph.element.ElementAdapterGridView;
import com.facebookproject.graph.element.Elements;
public class GetListTask extends AsyncTask<Void, Void, List<Elements>>{

	Holder fbHolder = new Holder();
	List<Elements> list = new ArrayList<Elements>();
	public GetListTask(Holder fbHolder){
		this.fbHolder = fbHolder;
	}
	@Override
	protected List<Elements> doInBackground(Void... params) {
		if(isCancelled())return null;

		switch(fbHolder.tab){
		case TAB_ME:
			switchtablistme();
			break;
		case TAB_FRIEND:
			switchtablistfriends();
			break;
		case TAB_LOCATION:
			switchtablistlocation();
			break;
		default:
			return null;
		}
		return list;
	}


	private void switchtablistme() {
		switch (fbHolder.type) {
		case ABOUT_ME:
			break;
		case MY_FOAVOURITES:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,category,likes,talking_about_count", "me/likes");
			break;
		case EVENTS:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,location,start_time,end_time,owner,cover", "me/events");
			break;
		case ALBUM:
			list = new GetAlbums().getlist(fbHolder.context, isCancelled(), "id,name,privacy,type,count", "me/albums");
			break;
		case MUSIC_I_LIKE:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,likes,talking_about_count", "me/music");
			break;
		case MOVIE_I_LIKE:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,likes,talking_about_count", "me/movies");
			break;
		case GAME_I_LIKE:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,likes,talking_about_count", "me/games");
			break;
		case BOOK_I_LIKE:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,likes,talking_about_count", "me/books");
			break;
		case TV_I_LIKE:
			list = new GetLikes().getlist(fbHolder.context, isCancelled(), "id,name,likes,talking_about_count", "me/television");
			break;
		case PHOTO_I_LIKE:
			break;
		case POST_I_LIKE:
			break;
		default:
			break;
		}
	}
	
	private void switchtablistfriends() {
		switch (fbHolder.type) {
		case MY_FRIENDS:
			list = new GetFriendsList().getmyfriendslist(fbHolder,isCancelled());
			break;
		default:
			break;
		}
	}
	
	private void switchtablistlocation() {
		switch (fbHolder.type) {
		case RESTAURANT:
			list = new GetRestaurantNearby().getrestaurantsnearby(fbHolder,isCancelled());
			break;
		case COFFEE:
			break;
		case CINEMA:
			list = new GetRestaurantNearby().getrestaurantsnearby(fbHolder,isCancelled());
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onPostExecute(List<Elements> result) {
		if(!result.isEmpty()){
			fbHolder.prgBar.setVisibility(View.GONE);
			fbHolder.adapter_list = new ElementAdapterListView(fbHolder.context, R.layout.element_list, result);
			fbHolder.adapter_grid = new ElementAdapterGridView(fbHolder.context, R.layout.element_grid, result);
			fbHolder.listView.setAdapter(fbHolder.adapter_list);
		}else{
			fbHolder.prgBar.setVisibility(View.GONE);
			Toast.makeText(fbHolder.context,"No item..",Toast.LENGTH_LONG).show();
		}
		super.onPostExecute(result);
	}
	
	@Override
	protected void onPreExecute() {
		fbHolder.prgBar.setVisibility(View.VISIBLE);
		super.onPreExecute();
	}
	@Override
	protected void onCancelled() {
		super.onCancelled();
	}


}
